let url='http://localhost:8080'             //全局url
$.ajaxSetup({
    async:false,
    dataType:"json",
})
//------------------------Activity.html---------------------------
// 查询 Activity 所有饭局
function getActivityAll() {
    let ActivityAll=[]
    $.post(url+'/ActivityServlet',{type:'selectAll'},function a(result) {
        ActivityAll = result.data
    },"json")
    return ActivityAll
}

// 查询 某个人发布的 Activity 所有饭局  （参数：用户id）
async function getActivityByUid(uid) {
    let ActivityAll=getActivityAll()
    let ActivityByUid=[]
    for(let i=0;i<ActivityAll.length;i++){
        if(ActivityAll[i].push_id==uid){
            ActivityByUid.push(ActivityAll[i])
        }
    }
    return ActivityByUid
}

//------------------------Release.html---------------------------
// 判断该用户是否又资格发布饭局
function getapplyHost(zh) {
    let hostShow = []
    let tag=false
    $.post(url + '/hostServlet', {
        type: 'selectAll',
    }, function (result) {
        hostShow = result.data
        for (let i = 0; i < hostShow.length; i++) {
            if (hostShow[i].user_id==zh) {
                tag=true
            }
        }
    }, "json")
    return tag
}
//------------------------Host.html---------------------------
// 显示该用户的所有组织的饭局
function getHostshow(uid) {
    let hostShow=[]
    $.post(url+'/FanJuJoinServlet',{
        type:'hostShow',
        userid:uid
    },function (result) {
        hostShow=result.data
    },"json")
    return hostShow
}
//------------------------Management.html---------------------------
//  显示报名人数
function getManaShow(uid,fanjuId) {
    let ManaShow=[]
    // alert(fanjuId)
    $.post(url+'/FanJuJoinServlet',{
        type:"ManaShow",
        userid:uid,
        fanjuid:fanjuId
    },function (result) {
        if(result.success==false)
            ManaShow=result.message
        else
            ManaShow=result.data
    })
    // alert(JSON.stringify(ManaShow))
    return ManaShow
}
//  选择或拒绝之后更新信息
function updateCode(uid,fanjuId,tag) {
    $.post(url+'/FanJuJoinServlet',{
        type:"updateCode",
        userid:uid,
        fanjuid:fanjuId,
        tag:tag
    })
}
//------------------------active.html---------------------------
// 查询 Activity 所有饭局
function getActivityAll222() {
    let ActivityAll222=[]
    $.post(url+'/ActivityServlet',{type:'selectAll222'},function a(result) {
        ActivityAll222 = result.data
    },"json")
    return ActivityAll222
}
//  搜索按钮通过地址、名称、时间
function getserach(search,list){
    let activity=[]
    // var reg = new RegExp(search);
    for(var i=0;i<list.length;i++){
        // 方法1
        // 如果字符串中不包含目标字符会返回-1
        if(JSON.stringify(list[i]).indexOf(search)>=0){
            activity.push(list[i]);
        }

        //方法2
        // if(JSON.stringify(list[i]).match(reg)){
        //     activity.push(list[i]);
        // }
    }
    // alert(JSON.stringify(activity))
    return activity
}
//  查询最火热的饭局
function getMaxactive(){
    let Maxactive=[]
    $.post(url+'/FanJuJoinServlet',{type:"getMaxactive"},function (typeResult) {
            Maxactive = typeResult.data
    },"json")
    return Maxactive
}
//  查询所有餐厅的地点
function getPlaces() {
    let msg = null
    $.post(url+"/RestaurantServlet",{type:"gtype"}, function (data){
        msg=data.data
    },"json")
    return msg
}

//  查询餐厅所有的类型
function getTypes(){
    let temp =null
    $.post(url+'/TypeServlet',{},function (typeResult,status) {
        if(status =="success"){
            temp = typeResult.data
        }else{
            alert('没获取到')
        }
    },"json")
    return temp
}
//------------------------Dinner.html---------------------------
//通过账号id获取参加的饭局
function getFanJuById(uid){
    let dinner =null
    $.post(url+'/FanJuJoinServlet',{type:"dinnerShow",userid:uid},function (Result,status) {
        if(status =="success"){
            dinner = Result.data
        }else{
            alert('没获取到')
        }
    },"json")
    return dinner
}

//------------------------index.html---------------------------
function getactivityHot() {
    let hot =null
    $.post(url+'/FanJuJoinServlet',{
        type:"getactivityHot"
    },function (Result,status) {
        if(status =="success"){
            hot = Result.data
        }else{
            alert('没获取到')
        }
    },"json")
    // alert(JSON.stringify(hot))
    return hot
}

//------------------------获取地址栏里面的参数---------------------------
function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return decodeURIComponent(r[2]);
    return null;
}
//------------------------js实现倒计时---------------------------
function showtime() {
    var nowtime = new Date(),  //获取当前时间
        endtime = new Date("2021-03-05 15:31:00");  //定义结束时间
    var lefttime = endtime.getTime() - nowtime.getTime(),  //距离结束时间的毫秒数
        leftd = Math.floor(lefttime/(1000*60*60*24)),  //计算天数
        lefth = Math.floor(lefttime/(1000*60*60)%24),  //计算小时数
        leftm = Math.floor(lefttime/(1000*60)%60),  //计算分钟数
        lefts = Math.floor(lefttime/1000%60);  //计算秒数
    return leftd + "天" + lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
    setInterval("showtime()",1000);
    }

//------------------------关注api---------------------------
function selectBycareId(carid) {
    let fans =[]                                //根据被关注的用户id查找用户id       查找粉丝
    $.post(url+'/CareServlet',{
        type:"selectBycareId",
        care_id:carid
    },function (Result,status) {
        if(status =="success"){
            fans = Result.data
        }else{
            alert('没获取到')
        }
    },"json")
    // alert(JSON.stringify(fans))
    return fans
}

function addCare(care,user) {
    let a = false
    $.post(url+'/CareServlet',{
        type:"addCare",
        care_zh:care,
        user_zh:user,
    },function (Result) {
        if(Result.code ==0){
            a = true
            // alert('post'+a)
        }else{
            alert('没获取到')
        }
    },"json")
    return a
}

//------------------------收藏---------------------------
function SelectAllRecord(care,user) {
    let a
    $.post(url+'/RecordServlet',{
        type:"selectAll",
    },function (Result) {
        if(Result.code ==0){
            a = Result.data
            // alert('post '+a)
        }else{
            alert('没获取到')
        }
    },"json")
    // alert(JSON.stringify(a))
    return a
}

function addHoutaiRecord(activity_id,user_id,tag) {
    let a
    $.post(url+'/RecordServlet',{
        type:"addHoutai",
        activity_id:activity_id,
        user_id:user_id,
        tag:tag,
    },function (Result) {
        if(Result.code ==0){
            a = Result.data
            // alert('post '+a)
        }else{
            // alert('没获取到')
        }
    },"json")
    // alert(JSON.stringify(a))
    return a
}

function updateHoutaiRecord(activity_id,user_id,tag,id) {
    let a
    // alert(id)
    $.post(url+'/RecordServlet',{
        type:"updateHoutai",
        activity_id:activity_id,
        user_id:user_id,
        tag:tag,
        id:id,
    },function (Result) {
        if(Result.code ==0){
            a = Result.data
            // alert('post '+a)
        }else{
            alert('没获取到')
        }
    },"json")
    // alert(JSON.stringify(a))
    return a
}


export default {
    getActivityAll,
    getActivityAll222,
    getActivityByUid,
    getHostshow,
    getManaShow,
    updateCode,
    getPlaces,
    getTypes,
    getFanJuById,
    getMaxactive,
    getserach,
    getactivityHot,
    getUrlParms,
    showtime,
    //关注
    selectBycareId,
    addCare,
    //收藏
    SelectAllRecord,
    addHoutaiRecord,
    updateHoutaiRecord,
}